import 'package:excel_utils/home_page.dart';
import 'package:excel_utils/pages/daily/daily_data_entry/daily_data_entry_page.dart';
import 'package:excel_utils/pages/week/week_entry/week_entry_page.dart';
import 'package:excel_utils/repository/constant.dart';
import 'package:excel_utils/repository/daily_entry/daily_entry_repository.dart';
import 'package:excel_utils/repository/k8/k8_repository.dart';
import 'package:excel_utils/repository/week/dis_customer_record_repository.dart';
import 'package:excel_utils/repository/week/monthly_package_transfer_repository.dart';
import 'package:excel_utils/repository/week/quotation_record_repository.dart';
import 'package:excel_utils/repository/week/rincos_repository.dart';
import 'package:excel_utils/service/DioManager.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:url_strategy/url_strategy.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  setPathUrlStrategy();
  // 允许横屏和竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.landscapeRight, DeviceOrientation.landscapeLeft]);

  runApp(const MyApp());
  // runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    // 添加观察者
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // 监听应用生命周期
    // if (state == AppLifecycleState.detached) {
    //   print('应用进入后台');
    // } else if (state == AppLifecycleState.inactive) {
    //   print('应用处于空闲状态');
    // } else if (state == AppLifecycleState.paused) {
    //   print('应用进入后台');
    // } else if (state == AppLifecycleState.resumed) {
    //   print('应用回到前台');
    // }
  }

  @override
  Widget build(BuildContext context) {
    return MultiRepositoryProvider(
        providers: [
          RepositoryProvider(create: (_) => K8Repository()),
          RepositoryProvider(create: (_) => DailyEntryRepository()),
          RepositoryProvider(create: (_) => QuotationRecordRepository()),
          RepositoryProvider(create: (_) => DisCustomerRecordRepository()),
          RepositoryProvider(create: (_) => MonthlyPackageTransferRepository()),
          RepositoryProvider(create: (_) => RincosRepository()),
        ],
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          navigatorKey: navigatorKey,
          title: 'xlsx',
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('zh', ''), // 支持中文
            Locale('en', ''), // 也可以支持英文或其他语言
          ],
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
            useMaterial3: true,
            elevatedButtonTheme: ElevatedButtonThemeData(
              style: ButtonStyle(
                backgroundColor: const WidgetStatePropertyAll(Colors.white),
                padding: const WidgetStatePropertyAll(EdgeInsets.symmetric(horizontal: 11, vertical: 6)),
                shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(4), side: const BorderSide(color: Colors.blue))),
                textStyle: const WidgetStatePropertyAll(TextStyle(color: Colors.white, fontSize: 16)),
              ),
            ),
            appBarTheme: const AppBarTheme(backgroundColor: Colors.white),
            scaffoldBackgroundColor: Colors.white,
          ),
          home: const HomePage(),
          onGenerateRoute: (settings) {
            if (settings.name!.contains("daily_data_entry")) {
              return MaterialPageRoute(builder: (context) => DailyDataEntryPage());
            } else if (settings.name!.contains("week_entry_data")) {
              return MaterialPageRoute(builder: (context) => WeekEntryPage());
            }
            return null;
          },
        ));
  }
}
